﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MododorWebAPI.DbContextConfig;
using MododorWebAPI.DTO;
using MododorWebAPI.Models;

namespace MododorWebAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly MododorDbContext mododorDb;

        public UserController(MododorDbContext mododorDb)
        {
            this.mododorDb = mododorDb;
        }

        [HttpPost]
        public async Task<ActionResult<object>> AuthUser(UserModelAuthDTO userDTO)
        {
            var userEnable = mododorDb.User.Where(x => x.UserEnable == true);
            if (userEnable.Any(x=>x.UserName==userDTO.UserName))
            {
                UserModel user = await userEnable.SingleAsync(x => x.UserName == userDTO.UserName);
                if (user.UserPassword==userDTO.Password)
                {
                    return Ok(new { status = 200, msg = "登录成功",res=new { res=user.UserRole} });
                }
            }
            return Ok(new { status = 100, msg = "登录失败" , res = new { res = "" } });
        }
    }
}
